import {
  config
} from '../../config/index';

import {
  domain,
  http
} from '@/utils/request'


/** 获取商品列表 */
function mockFetchGoodsList(pageIndex = 1, pageSize = 20) {
  const {
    delay
  } = require('../_utils/delay');
  const {
    getGoodsList
  } = require('../../model/goods');
  return delay().then(() =>
    getGoodsList(pageIndex, pageSize).map((item) => {
      return {
        spuId: item.spuId,
        thumb: item.primaryImage,
        title: item.title,
        price: item.minSalePrice,
        originPrice: item.maxLinePrice,
        tags: item.spuTagList.map((tag) => tag.title),
      };
    }),
  );
}

/** 获取商品列表 */
export function fetchGoodsList(pageIndex = 1, pageSize = 20, keyWords = '', tab, userId) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize);
  }
  return http('POST', '/goods/list', {
    keyWords,
    pageNo: pageIndex,
    pageSize,
    status: 1,
    reviewStatus: tab,
    userId,
  })
}


export function fetchGoodsListHome(pageIndex = 1, pageSize = 20, keyWords = '', tab, userId) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize);
  }
  return http('POST', '/goods/list', {
    keyWords,
    pageNo: pageIndex,
    pageSize,
    status: tab,
    userId,
  })
}

/** 获取竞价商品列表 */
export function fetchAuctionGoodsList(pageIndex = 1, pageSize = 20, keyWords = '', tab, userId) {
  if (config.useMock) {
    return mockFetchGoodsList(pageIndex, pageSize);
  }
  return http('POST', '/auction/myAuction', {
    reviewStatus: tab,
    keyWords,
    userId,
    pageNo: pageIndex,
    pageSize
  })
}

/** 获取竞价商品 */
export function fetchAuctionDetail(id) {
  return http('POST', '/goods/detail', {
    id
  })
}